<?php
class Todos extends Controller {

	function Todos()
	{
		parent::Controller();
		$this->users->check();
	}

	function index()
	{
		$template['title'] = 'Todos';
		// Get the uncompleted todos
		$data['query'] = $this->db->query("SELECT *, FROM_UNIXTIME(todos.bydate, '%M %e') AS thedate  FROM todos LEFT JOIN users ON users.id = todos.assignto WHERE todos.completed = '0' ORDER BY todos.bydate ASC");
		
		// Get the completed todos
		$data['completed'] = $this->db->query("SELECT *, FROM_UNIXTIME(todos.completedate, '%M %e') AS thedate FROM todos LEFT JOIN users ON users.id = todos.assignto WHERE todos.completed = '1'");
		
		// Get the users for the form
		$data['users'] = $this->db->query('SELECT * FROM users');
		
		$template['content'] = $this->load->view('todos', $data, TRUE);
		
		// The calender for the form
		$template['headers'] = '<script>
  $(document).ready(function(){
$("#date").datepicker({dateFormat: "yy-mm-dd", });
  });
  </script>
';

		$this->load->view('template', $template);
		
	}
	function add()
	{
		$data['query'] = $this->db->query('SELECT * FROM users');
		$template['content'] = $this->load->view('todo_add', $data, TRUE);
		$template ['tabs'] = array(
			anchor('todos', 'Back to todos'),
		);
		$template['headers'] = '<script>
  $(document).ready(function(){
$("#date").datepicker({dateFormat: "yy-mm-dd", });
  });
  </script>
';
		$this->load->view('template', $template);
	}
	function do_add() 
	{
		$this->db->set('description', $_POST['todo']);
		$this->db->set('bydate', human_to_unix($_POST['date'] . " 12:00:00 AM"));
		$this->db->set('assigndate', now());
		$this->db->set('assignto', $_POST['user']);
		$this->db->set('completed', '0');
		$this->db->insert('todos');
		redirect('todos');
	}
	function complete()
	{
		$this->db->set('completed', '1');
		$this->db->set('completedate', now());
		$this->db->where('todo', $this->uri->segment(3));
		$this->db->update('todos');
		redirect('todos');
	}
	function uncomplete()
	{
		$this->db->set('completed', '0');
		$this->db->where('todo', $this->uri->segment(3));
		$this->db->update('todos');
		redirect('todos');
	}
	function deleteall()
	{
		$this->db->truncate('todos');
		redirect('todos');
	}
	function delete()
	{
		//$this->db->query('DELETE FROM todos WHERE todo=' . $this->uri->segment(3));
		$this->db->where('todo', $this->uri->segment(3));
		$this->db->delete('todos');
		redirect('todos');
	}
	function edit() 
	{
		$this->db->where('todo', $this->uri->segment(3));
		$data['user'] = $this->db->query('SELECT * FROM users');
		$data['query'] = $this->db->get('todos');
		$template ['tabs'] = array(
			anchor('todos', 'Back to todos'),
		);
		$template['headers'] = '<script>
  $(document).ready(function(){
$("#date").datepicker({dateFormat: "yy-mm-dd", });
  });
  </script>
';
		$template['content'] = $this->load->view('todos_edit', $data, TRUE);
		$this->load->view('template', $template);
	}
	function save()
	{
		$this->db->set('description', $_POST['description']);
		$this->db->set('assignto', $_POST['user']);
		$this->db->set('bydate', human_to_unix($_POST['bydate'] . " 12:00:00 AM"));
		$this->db->where('todo', $_POST['todo']);
		$this->db->update('todos');
		
		redirect('todos');
	}
}